package com.zrrd.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.article.mapper.ApArticleConfigMapper;
import com.zrrd.article.mapper.ApArticleContentMapper;
import com.zrrd.article.mapper.ApArticleMapper;
import com.zrrd.article.service.ApArticleService;
import com.zrrd.article.service.ArticleFreemarkerService;
import com.zrrd.common.constants.ArticleConstants;
import com.zrrd.model.article.dtos.ArticleDto;
import com.zrrd.model.article.dtos.ArticleHomeDto;
import com.zrrd.model.article.pojos.ApArticle;
import com.zrrd.model.article.pojos.ApArticleConfig;
import com.zrrd.model.article.pojos.ApArticleContent;
import com.zrrd.model.common.dtos.ResponseResult;
import com.zrrd.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author WanJl
 * @version 1.0
 * @title ApArticleServiceImpl
 * @description
 * @create 2023/9/18 12:16
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;


    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;

    //单页最大加载数字
    private final static short MAX_PAGE_SIZE=50;
    /**
     * 根据参数加载文章列表
     *
     * @param dto
     * @param loadType 1：加载更多新闻，2：加载最新新闻。"__all__"：加载全部新闻
     * @return
     */
    @Override
    public ResponseResult load(Short loadType,ArticleHomeDto dto) {
        //1. 参数校验
        //1.1 基本校验
        Integer size = dto.getSize();
        if (size==null||size==0){
            size=10;
        }
        size = Math.min(size, MAX_PAGE_SIZE);
        dto.setSize(size);
        //1.2 类型参数校验
        if (!loadType.equals(ArticleConstants.LOADTYPE_LOAD_NEW)&&!loadType.equals(ArticleConstants.LOADTYPE_LOAD_MORE)){
            loadType=ArticleConstants.LOADTYPE_LOAD_MORE;
        }
        //1.3 新闻频道校验
        if (StringUtils.isEmpty(dto.getTag())){
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        //1.4 时间校验
        if (dto.getMaxBehotTime()==null){
            dto.setMaxBehotTime(new Date());
        }
        if (dto.getMinBehotTime()==null){
            dto.setMinBehotTime(new Date());
        }
        //2.查询数据
        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, loadType);

        //3.封装结果
        ResponseResult responseResult = ResponseResult.okResult(apArticles);
        return responseResult;
    }

    /**
     * 保存APP端相关的新闻
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        //0.检查参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //1.保存新闻
        ApArticle apArticle = new ApArticle();
        //拷贝dto里所有属性值到apArticle对象
        BeanUtils.copyProperties(dto,apArticle);
        //判断是否存在id
        if (dto.getId()==null){
            //1.1 新增
            //保存新闻
            save(apArticle);
            //2.保存新闻配置
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(apArticleConfig);

            //3.保存新闻内容
            ApArticleContent apArticleContent = new ApArticleContent();
            //设置新闻内容表中的新闻id
            apArticleContent.setArticleId(apArticle.getId());
            //添加新闻内容
            apArticleContent.setContent(dto.getContent());
            //调用apArticleContentMapper执行插入操作
            apArticleContentMapper.insert(apArticleContent);


        }else {
            //1.2 修改
            //修改新闻
            updateById(apArticle);
            //3.更新新闻内容
            //根据新闻id查询新闻内容表，是否有相应的记录
            LambdaQueryWrapper<ApArticleContent> lqw=new LambdaQueryWrapper<>();
            lqw.eq(ApArticleContent::getArticleId,dto.getId());
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(lqw);
            //修改新闻内容
            apArticleContent.setContent(dto.getContent());
            //调用mapper执行更新
            apArticleContentMapper.updateById(apArticleContent);
        }
        //异步调用，生成静态文件并且上传到MinIO
        articleFreemarkerService.buidArticleToMinIO(apArticle,dto.getContent());

        //4.返回结果，返回新闻的id
        return ResponseResult.okResult(apArticle.getId());
    }
}
